package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.BonusPoint;
import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.mapper.BonusPointMapper;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class SignServiceImpl implements ISignService{

    @Autowired
    private SignMapper signMapper;

    @Autowired
    private BonusPointMapper bonusPointMapper;

    @Override
    public Integer CheckSignByUserId(long id) {

        if (signMapper.CheckSignByUserId(id) == null){
            return 0;
        }
        return signMapper.CheckSignByUserId(id);
    }

    @Override
    public Object getSigncountByUserId(Long id) {

       return signMapper.getSigncountByUserId(id);
    }

    @Override
    public void saveOrUpdate(Sign sign) {
        BonusPoint bonusPoint = new BonusPoint();
        if(signMapper.selectByUserId(sign.getUser_id()) == null){
            sign.setCount(1);
            sign.setSignTime(new Date());
            signMapper.insert(sign);
            bonusPoint.setUserId(sign.getUser_id());
            bonusPoint.setPoint(1* 10);
            bonusPointMapper.insert(bonusPoint);
            return;
        }
        sign.setSignTime(new Date());
        int count = signMapper.selectByUserId(sign.getUser_id()).getCount() + 1;
        sign.setCount(count);
        bonusPoint.setUserId(sign.getUser_id());
        Integer point = bonusPointMapper.getPointByUserId(sign.getUser_id()).getPoint();
        bonusPoint.setPoint(10 + point);
        bonusPointMapper.updatePointByUserId(bonusPoint);
        signMapper.updateByUserId(sign);

    }
}
